package com.uzai.console.service.user.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.uzai.common.enums.MerConfigKeyEnum;
import com.uzai.common.enums.MerConfigTypeEnum;
import com.uzai.common.enums.RedisCacheKeyEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.dto.user.wechatblacklist.WechatBlacklistBaseDto;
import com.uzai.console.entity.MerBaseConfig;
import com.uzai.console.entity.redis.WechatBlacklistBaseRedis;
import com.uzai.console.mapper.CikuKeywordsMapper;
import com.uzai.console.mapper.CikuKeywordsReplyMapper;
import com.uzai.console.mapper.MerBaseConfigMapper;
import com.uzai.console.service.user.WechatBlacklistBaseService;
import com.uzai.console.vo.user.wechatblacklist.WechatBlacklistBaseVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author liuqi
 * @date 2021年08月23日 20:25
 */
@Service
public class WechatBlacklistBaseServiceImpl implements WechatBlacklistBaseService {

    @Autowired
    private MerBaseConfigMapper merBaseConfigMapper;
    @Autowired
    private CikuKeywordsReplyMapper cikuKeywordsReplyMapper;
    @Autowired
    private CikuKeywordsMapper cikuKeywordsMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询云黑名单基础设置
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return null
     */
    public WechatBlacklistBaseVo findWechatBlacklistConfig(Long merId){
        WechatBlacklistBaseVo wechatBlacklistBaseVo = new WechatBlacklistBaseVo();
        //修改会员黑名单配置
        MerBaseConfig user_blacklist_config = merBaseConfigMapper.selectByMerId(merId, MerConfigKeyEnum.USER_BLACKLIST_CONFIG.getValue());
        if(user_blacklist_config != null && StringUtils.isNotBlank(user_blacklist_config.getVal())){
            wechatBlacklistBaseVo =  JSONObject.parseObject(user_blacklist_config.getVal(), WechatBlacklistBaseVo.class);
        }
        return wechatBlacklistBaseVo;
    }

    /**
     * 修改云黑名单基础设置
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param wechatBlacklistBaseDto
     * @return null
     */
    @Transactional
    public void updateWechatBlacklistConfig(WechatBlacklistBaseDto wechatBlacklistBaseDto){

        if(wechatBlacklistBaseDto.getBlacklistPrvSwitch() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入私有黑名单配置");
        }

        if(wechatBlacklistBaseDto.getBlacklistPubSwitch() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入全网黑名单配置");
        }

//        if(wechatBlacklistBaseDto.getBlacklistpPeerSwitch() == null){
//            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入同行黑名单配置");
//        }

        if(wechatBlacklistBaseDto.getAutoAddFriendBlacklistParentSwitch() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入私有黑名单用户推荐的下级不自动通过好友开关配置");
        }

        if(wechatBlacklistBaseDto.getAutoAddFriendBlacklistPubParentSwitch() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入全网黑名单用户推荐的下级不自动通过好友开关配置");
        }

        if(wechatBlacklistBaseDto.getAutoAddFriendParentNoOrderSwitch() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入上级无订单下级不自动通过好友开关配置");
        }

        Integer now = DateUtil.getNowTime();
        //修改会员黑名单配置
        MerBaseConfig user_blacklist_config = merBaseConfigMapper.selectByMerId(wechatBlacklistBaseDto.getMerId(), MerConfigKeyEnum.USER_BLACKLIST_CONFIG.getValue());
        if (user_blacklist_config != null){
            user_blacklist_config.setVal(Tools.getStrEmpty(JSONObject.toJSONString(wechatBlacklistBaseDto)));
            user_blacklist_config.setUpdateTime(now);
            merBaseConfigMapper.update(user_blacklist_config);
        } else { //增加会员黑名单配置
            user_blacklist_config = new MerBaseConfig();
            user_blacklist_config.setId(IdWorker.getId());
            user_blacklist_config.setMerId(wechatBlacklistBaseDto.getMerId());
            user_blacklist_config.setType(MerConfigTypeEnum.PRODUCTS.getValue());
            user_blacklist_config.setKey(MerConfigKeyEnum.USER_BLACKLIST_CONFIG.getValue());
            user_blacklist_config.setVal(Tools.getStrEmpty(JSONObject.toJSONString(wechatBlacklistBaseDto)));
            user_blacklist_config.setCreateTime(now);
            user_blacklist_config.setUpdateTime(now);
            merBaseConfigMapper.insert(user_blacklist_config);
        }

        //保存到Redis当中
        WechatBlacklistBaseRedis wechatBlacklistBaseRedis = new WechatBlacklistBaseRedis();
        BeanUtils.copyProperties(wechatBlacklistBaseDto, wechatBlacklistBaseRedis);
        redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.USER_BLACKLIST_CONFIG.getKey(), wechatBlacklistBaseDto.getMerId()), Tools.getStrEmpty(JSONObject.toJSONString(wechatBlacklistBaseRedis)));

    }

}
